﻿using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace I2kCoreEngine
{
    public static class Utilities
    {
        #region Supported File Types Enumeration
        public enum FileTypes
        {
            Word,
            Excel
        }

        #endregion

        #region Supported Languages Enumeration

        public enum Languages
        {
            Hebrew,
            English
        }

        #endregion

        #region string[] FindFilesInDirectory(...) List all the files in a specified directory
        public static string[] FindFilesInDirectory ( string directoryToExplore, FileTypes fileType, bool isRecursive )
        {
            if (!Directory.Exists( directoryToExplore ))
                throw new DirectoryNotFoundException( );

            var searchPattern = new StringWriter( );

            switch (fileType)
            {
                case FileTypes.Word:
                    searchPattern.Write( "*.doc*" );
                    break;
                case FileTypes.Excel:
                    searchPattern.Write( "*.xls*" );
                    break;
            }

            string[] filesFound;

            if (isRecursive)
                filesFound = Directory.GetFiles( directoryToExplore, searchPattern.ToString( ),
                                                SearchOption.AllDirectories );
            else
                filesFound = Directory.GetFiles( directoryToExplore, searchPattern.ToString( ),
                                                SearchOption.TopDirectoryOnly );
            return filesFound;
        }

        #endregion

        #region void SetKeyboardLanguage(...) Change the system input language
        public static void SetKeyboardLanguage ( Languages chosenLanguage )
        {
            string languageString = "en";
            switch (chosenLanguage)
            {
                case Languages.Hebrew:
                    languageString = "he";
                    break;
                case Languages.English:
                    languageString = "en";
                    break;
            }

            foreach (InputLanguage lang in
                InputLanguage.InstalledInputLanguages.Cast<InputLanguage>().Where(lang => lang.Culture.TwoLetterISOLanguageName == languageString))
            {
                InputLanguage.CurrentInputLanguage = lang;
            }
        }

        #endregion

        #region float CalcFreeRam ( ) Calculate system available memory
        public static float CalcFreeRam ( )
        {
            PerformanceCounter ramCounter = new PerformanceCounter( "Memory", "Available MBytes" );
            return (ramCounter.NextValue( ));
        }
        #endregion
    }
}